package edu.iss.demo.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import edu.iss.demo.exception.EmployeeNotFound;
import edu.iss.demo.exception.StudentNotFound;
import edu.iss.demo.model.Employee;
//import edu.iss.demo.model.Student;
import edu.iss.demo.service.EmployeeService;

//import edu.iss.demo.service.StudentService;
@Controller
@RequestMapping(value = "/employee")
public class EmployeeController {
	@Autowired
	private EmployeeService employeeService;

	private List<Employee> emList = null;
	private List<String> roleList = new ArrayList<String>();

	private void init() {
		if (roleList.size() == 0) {
			roleList.add("Employee");
			roleList.add("Manager");

		}
	}

	/*
	 * @Autowired private StudentValidator studentValidator;
	 * 
	 * @InitBinder private void initBinder(WebDataBinder binder) {
	 * binder.setValidator(studentValidator); }
	 */

	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public ModelAndView newEmployeePage() {

		ModelAndView mav = new ModelAndView("Employee-create", "Employee",
				new Employee());
		init();
		List<Employee> elist = employeeService.findManagerList();
		mav.addObject("empList", elist);
		mav.addObject("roleList", roleList);
		return mav;
	}

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public ModelAndView createNewEmployee(
			@ModelAttribute("Employee") @Valid Employee employee,
			BindingResult result, final RedirectAttributes redirectAttributes) {

		System.out.println("creating employee of NAME-->" + employee.getName());

		emList = null;

		if (employee.getReportingTo() != null) {
			System.out.println("creating employee of NAME-->"
					+ employee.getReportingTo().getEmployeeId());
			employee.setReportingTo(getEmList().get(
					(int) employee.getReportingTo().getEmployeeId()));
		}

		if (result.hasErrors())
			return new ModelAndView("Employee-create");

		employeeService.create(employee);

		ModelAndView mav = new ModelAndView();
		String message = "New employee " + employee.getName()
				+ " was successfully created.";

		mav.setViewName("AdminHome");

		redirectAttributes.addFlashAttribute("message", message);
		return mav;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView employeeListPage() {
		ModelAndView mav = new ModelAndView("Employee-list");
		List<Employee> empList = employeeService.findAll();
		mav.addObject("empList", empList);
		return mav;
	}

	public List<Employee> getEmList() {

		if (emList == null) {
			emList = employeeService.findAll();
		}

		return emList;
	}

	public void setEmList(List<Employee> emList) {
		this.emList = emList;
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public ModelAndView editEmployeePage(@PathVariable Long id) {
		ModelAndView mav = new ModelAndView("Employee-edit");
		Employee employee = employeeService.findById(id);
		mav.addObject("Employee", employee);
		List<Employee> empList = getEmList();
		mav.addObject("empList", empList);
		return mav;
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
	public ModelAndView editEmployee(@ModelAttribute @Valid Employee employee,
			BindingResult result, @PathVariable Long id,
			final RedirectAttributes redirectAttributes)
			throws EmployeeNotFound {
		System.out.println(id);
		employee.setEmployeeId(id);
		if (result.hasErrors())
			return new ModelAndView("Employee-edit");

		ModelAndView mav = new ModelAndView("AdminHome");
		String message = "Employee " +employee.getName()+" was successfully updated.";
		
		employeeService.update(employee);

		redirectAttributes.addFlashAttribute("message", message);
		return mav;
	}

	/*
	 * @RequestMapping(value = "/list", method = RequestMethod.GET) public
	 * ModelAndView studentListPage() { ModelAndView mav = new
	 * ModelAndView("student-list"); List<Student> studentList =
	 * studentService.findAll(); mav.addObject("studentList", studentList);
	 * return mav; }
	 */

	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public ModelAndView deleteEmployee(@PathVariable Integer id,
			final RedirectAttributes redirectAttributes)
			throws EmployeeNotFound {

		ModelAndView mav = new ModelAndView("AdminHome");

		Employee employee = employeeService.delete(id);
		String message = "The employee " + employee.getName()
				+ " was successfully deleted.";

		redirectAttributes.addFlashAttribute("message", message);
		return mav;
	}

	/*
	 * @RequestMapping(value = "/list", method = RequestMethod.GET) public
	 * ModelAndView studentListPage() { ModelAndView mav = new
	 * ModelAndView("student-list"); List<Student> studentList =
	 * studentService.findAll(); mav.addObject("studentList", studentList);
	 * return mav; }
	 */
}
